package com.spring.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot.entity.SysRole;
import com.spring.boot.entity.SysUserRole;
import com.spring.boot.entity.UserInfo;
import com.spring.boot.mapper.UserMapper;
//import com.spring.boot.service.UserService;
import com.spring.boot.paramMap.UserMap;
import com.spring.boot.service.SysRoleService;
import com.spring.boot.utils.MyUtils;
import com.spring.boot.vo.UserVo;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserInfo>{
    private final static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Bean
    public PasswordEncoder passwordEncoder(){
        //使用BCrypt加密，写在配置类里最好
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SysUserRoleServiceImpl userRoleService;
    @Autowired
    private SysRoleService roleService;

    private final PasswordEncoder passwordEncoder = passwordEncoder();


    public UserInfo getUserByUsername(String username) {
        UserInfo user = userMapper.getUserByUsername(username);
        return user;
    }

    public void updateByUsername(UserInfo user) {
        userMapper.updateByUsername(user.getUsername(), user.getPassword());

    }

    public Page<UserVo> pageVo(Page<UserInfo> page, QueryWrapper<UserInfo> userQueryWrapper) {
        Page<UserInfo> oldPageList = userMapper.selectPage(page, userQueryWrapper);
        List<UserVo> newRecords = new ArrayList<>();
        //对旧pageList 的每一个user对象设置role
        for (UserInfo userInfo : oldPageList.getRecords()) {
            StringBuilder roleStr = new StringBuilder();
            List<SysUserRole> urList = userRoleService.getByUserId(userInfo.getId());
            for (SysUserRole userRole : urList) {
                SysRole role = roleService.getById(userRole.getRoleId());
                roleStr.append(role.getRole()).append(',');
            }
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userInfo,userVo);
            userVo.setRole(roleStr.toString());
            newRecords.add(userVo);
        }
        Page<UserVo> newPageList = new Page<>(oldPageList.getCurrent(), oldPageList.getSize(), oldPageList.getTotal());
        newPageList.setRecords(newRecords);
        return newPageList;
    }

    public boolean saveUser(UserMap userMap){
        String newPass = passwordEncoder.encode(userMap.getPassword());
        userMap.setPassword(newPass);
        //往user表里保存用户信息
//        UserInfo userInfo = new UserInfo();
//        BeanUtils.copyProperties(userMap,userInfo);
//        int insert = userMapper.insert(userInfo);
        int insert = userMapper.insert(userMap);
        if (insert == 1){
            //根据用户名获取用户id
            UserInfo userInfo = userMapper.getUserByUsername(userMap.getUsername());
            int id = userInfo.getId();
            userRoleService.insertByUserId(id,userMap.getRole());
            return true;
        }
        else {
            return false;
        }
//        log.info("新增的用户ID："+ insert);
        //新增该用户的角色信息

    }
}
